// Package trip 模型
package trip

import (
	// "gitee.com/sweetsunnyflower/carpooling-server-go/pkg/logger"

	"gitee.com/sweetsunnyflower/carpooling-server-go/app/models"
	"gitee.com/sweetsunnyflower/carpooling-server-go/app/models/region"
	"gitee.com/sweetsunnyflower/carpooling-server-go/app/models/trip_rider"
	"gitee.com/sweetsunnyflower/carpooling-server-go/app/models/vehicle"
	"gitee.com/sweetsunnyflower/carpooling-server-go/pkg/database"
)

const (
	_ = iota
	ING
	WAITING_FOR_DEPARTURE
	CANCEL
	FINISH
)

type User struct {
	Id     uint64 `json:"id"`
	Name   string `json:"name"`
	Avatar string `json:"avatar"`
}

type Driver struct {
	Id          uint64 `json:"id"`
	Year        uint   `json:"year"`
	LicenseType string `json:"license_type"`
}

type UserFollow struct {
	models.BaseModel

	UserId   uint64 `gorm:"column:user_id" db:"user_id" json:"user_id" form:"user_id"`         //关注者ID
	FollowId uint64 `gorm:"column:follow_id" db:"follow_id" json:"follow_id" form:"follow_id"` //被关注者ID
	Type     uint8  `gorm:"column:type;comment:1乘客关注司机 2司机关注乘客;" db:"type" json:"type" from:"type"`

	models.CommonTimestampsField
}

// 司机发布行程表
type Trip struct {
	models.BaseModel
	UserId         uint64                  `gorm:"column:user_id" db:"user_id" json:"user_id" form:"user_id"`
	DriverId       uint64                  `gorm:"column:driver_id" db:"driver_id" json:"driver_id" form:"driver_id"`
	VehicleId      uint64                  `gorm:"column:vehicle_id" db:"vehicle_id" json:"vehicle_id" form:"vehicle_id"` //车辆ID
	StartLocation  uint64                  `gorm:"column:start_location" db:"start_location" json:"start_location" form:"start_location"`
	EndLocation    uint64                  `gorm:"column:end_location" db:"end_location" json:"end_location" form:"end_location"`
	DepartureTime  models.FormatTime       `gorm:"column:departure_time" db:"departure_time" json:"departure_time" form:"departure_time"`     //出发时间
	Deadline       models.FormatTime       `gorm:"column:deadline" db:"deadline" json:"deadline" form:"deadline"`                             //拼车截止时间
	TotalSeats     uint8                   `gorm:"column:total_seats" db:"total_seats" json:"total_seats" form:"total_seats"`                 //总座位数
	AvailableSeats uint8                   `gorm:"column:available_seats" db:"available_seats" json:"available_seats" form:"available_seats"` //可用座位数
	Price          float64                 `gorm:"column:price" db:"price" json:"price" form:"price"`                                         //每个座位的价格
	Itinerary      string                  `gorm:"type:varchar(255)" db:"itinerary" json:"itinerary" form:"itinerary"`                        //行程安排
	Status         uint8                   `gorm:"column:status;comment:行程状态 1进行中 2等待发车 3司机取消 4已完成;" db:"status" json:"status" form:"status"` // 行程状态
	User           *User                   `json:"user"`
	Driver         *Driver                 `json:"driver"`
	Vehicle        *vehicle.Vehicle        `json:"vehicle"`
	From           *region.Region          `gorm:"foreignKey:start_location" json:"from"`
	To             *region.Region          `gorm:"foreignKey:end_location" json:"to"`
	BeFollow       []*UserFollow           `gorm:"foreignKey:follow_id;references:user_id;" json:"be_follow"`
	TripRider      []*trip_rider.TripRider `gorm:"foreignKey:trip_id;references:id;" json:"trip_rider"`

	models.CommonTimestampsField
}

// 司机发布行程表
type QueryTrip struct {
	Trip
	Followed bool `json:"followed"`
	Booked   bool `json:"booked"`
	Owner    bool `json:"owner"`
}

func (trip *Trip) Create() {
	database.DB.Create(&trip)
}

func (trip *Trip) Save() (rowsAffected int64) {
	result := database.DB.Save(&trip)
	return result.RowsAffected
}

func (trip *Trip) Delete() (rowsAffected int64) {
	result := database.DB.Delete(&trip)
	return result.RowsAffected
}
